﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Diagnostics;

namespace Arthus
{
    public partial class pocetnaForma : Form
    {
        aplikacijaForma forma2 = null;
        public pocetnaForma()
        {
            Thread t = new Thread(new ThreadStart(SplashScreen));
            t.Start();
            Thread.Sleep(3000);
            InitializeComponent();
            t.Abort();
        }
        //neophodne stavke za konekciju sa bazom
        MySqlConnection konekcija;
        MySqlCommand komanda = new MySqlCommand();
        MySqlDataReader datareader = null;
        MySqlDataAdapter adapter = null;

        private void pictureBoxLogo_Click(object sender, EventArgs e)
        {
            //klikom na sliku ide se na sajt firme
            Process.Start("http://www.agrovin.co.rs");
        }

        private void buttonLogin_Click(object sender, EventArgs e)
        {
            //poziv Login metode
            this.Cursor = Cursors.WaitCursor;
            Login();
            this.Cursor = Cursors.Default;
        }

        private void kreirajAplikacijuFormu()
        {
            forma2 = new aplikacijaForma();
        }

        public void Login()
        {
            String username = "";
            String password = "";
            try
            {
                //konekcija ka bazi i citanje redova
                konekcija = new MySqlConnection(statickePromenljive.konekcioniString);
                konekcija.Open();
                komanda.Connection = konekcija;
                komanda.CommandType = CommandType.Text;
                komanda.CommandText = "SELECT IME, PASS FROM korisnici WHERE IME='"
                    + textBoxUser.Text + "' AND PASS='" + textBoxPass.Text + "'";
                datareader = komanda.ExecuteReader();
                while (datareader.Read())
                {
                    username = datareader.GetValue(0).ToString();
                    password = datareader.GetValue(1).ToString();
                    statickePromenljive.ulogovan = username;
                }
                datareader.Close();
                //provera korisnickog imena i lozinke
                if (username == "" || password == "" || username == null || password == null || username.Length <= 0 || password.Length <= 0)
                {
                    labelPogresanLogin.Text = "Pogrešno korisničko ime ili lozinka, pokušajte ponovo";
                    textBoxUser.Focus();
                }
                else
                {
                    try
                    {
                        Arthus.Slike.Ucitavanje psf = new Arthus.Slike.Ucitavanje();
                        psf.Show();
                        this.Hide();
                        forma2 = new aplikacijaForma();
                        psf.Hide();
                        forma2.ShowDialog();
                        this.Close();
                        psf.Close();
                    }
                    catch (Exception ex) {
                        MessageBox.Show("Neočekivana greška!\n Kontaktirajte administratora...\n\n\n"+ex.ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Neočekivana greška! " + ex.ToString(), "Greška!", MessageBoxButtons.OK);
                Application.Exit();
            }
            finally
            {
                //zatvaranje konekcije
                konekcija.Close();
            }
        }

        public void PopuniFormu()
        {
            try
            {
                //konekcija sa bazom i punjenje dataset-a
                konekcija = new MySqlConnection(statickePromenljive.konekcioniString);
                komanda.Connection = konekcija;
                komanda.CommandText = "SELECT IME, PASS FROM korisnici WHERE IME='"+statickePromenljive.preduzeceKorisnik+"'";
                DataSet dataset = new DataSet();
                adapter = new MySqlDataAdapter();
                adapter.SelectCommand = komanda;
                adapter.Fill(dataset);

                textBoxUser.DataBindings.Add("Text", dataset.Tables[0], "IME");
                textBoxPass.DataBindings.Add("Text", dataset.Tables[0], "PASS");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Neočekivana greška! " + ex.ToString(), "Greška!", MessageBoxButtons.OK);
                Application.Exit();
            }
            finally
            {
                adapter.Dispose();
                konekcija.Close();
            }
        }

        private void TABKaoENTER(KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                e.SuppressKeyPress = true;
                SelectNextControl(ActiveControl, true, true, true, true);
            }
        }

        private void textBoxUser_KeyDown(object sender, KeyEventArgs e)
        {
            TABKaoENTER(e);
        }
        private void textBoxPass_KeyDown(object sender, KeyEventArgs e)
        {
            TABKaoENTER(e);
        }

        private void pocetnaForma_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        public void SplashScreen() {
            Application.Run(new pocetnaSlikaForma());
        }

        private void pocetnaForma_Load(object sender, EventArgs e)
        {
            PopuniFormu();
        }
    }
}
